/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = mysql_dissect_login.c                              */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/12 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <mysql/mysql_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/mysql/config.h"

extern int mysql_dissect_login(Conntrack *p_st_conn
                      , L7session_mysql_private *p_st_private
                      , Frhash_header *p_st_hash_report);

extern void mysql_destroy_session(L7session_mysql **pp_session);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql st_sess;
    char *p_data = (char *)malloc(10);
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(&st_sess, 0x00, sizeof(L7session_mysql));
    frhash2_create(&st_hash_report, 1024);
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 4;
    memcpy(p_data, (char *)"\xff\x00\x00\x02", 4);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 4);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = &st_sess;

    i_ret = mysql_dissect_login(NULL, &st_mysql, &st_hash_report);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    frhash2_free(&st_hash_report, NULL);
    free(p_data);
    bytebuff_free(&st_fragment.p_st_data);
    return;
}

static void case2(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql st_sess;
    char *p_data = (char *)malloc(10);
    Conntrack st_conn;
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(&st_sess, 0x00, sizeof(L7session_mysql));
    frhash2_create(&st_hash_report, 1024);
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 4;
    memcpy(p_data, (char *)"\xff\x00\x00\x02", 4);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 4);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = &st_sess;

    i_ret = mysql_dissect_login(&st_conn, NULL, &st_hash_report);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    frhash2_free(&st_hash_report, NULL);
    free(p_data);
    bytebuff_free(&st_fragment.p_st_data);
    return;
}

static void case3(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql st_sess;
    char *p_data = (char *)malloc(10);
    Conntrack st_conn;
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(&st_sess, 0x00, sizeof(L7session_mysql));
    frhash2_create(&st_hash_report, 1024);
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 4;
    memcpy(p_data, (char *)"\xff\x00\x00\x02", 4);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 4);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = &st_sess;

    i_ret = mysql_dissect_login(&st_conn, &st_mysql, NULL);
    
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    frhash2_free(&st_hash_report, NULL);
    free(p_data);
    bytebuff_free(&st_fragment.p_st_data);
    return;
}

static void case4(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql st_sess;
    char *p_data = (char *)frmalloc(128);
    Conntrack st_conn;
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(&st_sess, 0x00, sizeof(L7session_mysql));
    frhash2_create(&st_hash_report, 1024);
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 38;
    memcpy(p_data, (char *)"\x85\xa6\x03\x00\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x72\x6f\x6f\x74\x00\x00", 38);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 38);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = &st_sess;

    i_ret = mysql_dissect_login(&st_conn, &st_mysql, &st_hash_report);
    
    frfree(st_sess.p_db_user);
    frfree(st_sess.p_charset);
    frfree(st_mysql.p_st_req);
    //mysql_destroy_session(&st_sess);
    frfree(p_data);
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_SUCC);
    frhash2_free(&st_hash_report, NULL);
    bytebuff_free(&st_fragment.p_st_data);
    return;
}

static void case5(void)
{
    int i_ret = 0;
    L7session_mysql_private st_mysql;
    Frhash_header st_hash_report;
    L7_fragment_mysql st_fragment;
    L7session_mysql st_sess;
    char *p_data = (char *)frmalloc(128);
    Conntrack st_conn;
    
    memset(&st_mysql, 0x00, sizeof(L7session_mysql_private));
    memset(&st_conn, 0x00, sizeof(Conntrack));
    memset(&st_fragment, 0x00, sizeof(L7_fragment_mysql));
    memset(&st_sess, 0x00, sizeof(L7session_mysql));
    frhash2_create(&st_hash_report, 1024);
    
    st_fragment.u64_parsed_index = 1;
    st_fragment.u64_total_len = 5;
    memcpy(p_data, (char *)"\x85\xa6\x03\x00\x00", 5);
    st_fragment.p_st_data = bytebuff_creat();
    bytebuff_add(st_fragment.p_st_data, p_data, 5);
    st_mysql.p_st_fragment = &st_fragment;
    st_mysql.p_st_sess = &st_sess;

    i_ret = mysql_dissect_login(&st_conn, &st_mysql, &st_hash_report);
    
    frfree(p_data);
    CU_ASSERT_EQUAL(i_ret,APM_L7_ANALYZE_FAIL);
    frhash2_free(&st_hash_report, NULL);
    bytebuff_free(&st_fragment.p_st_data);
    frfree(st_mysql.p_st_req);
    return;
}

int add_suite_mysql_dissect_login(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" mysql_dissect_login()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
